package com.mvp.dagger.converter;

import android.app.Application;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.TypeAdapter;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Converter;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2018/9/11.
 */
public class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Application application;
    private final Gson gson;
    private final TypeAdapter<T> adapter;
    private final Type type;

    GsonResponseBodyConverter(Application application, Gson gson, TypeAdapter<T> adapter, Type type) {
        this.application = application;
        this.gson = gson;
        this.adapter = adapter;
        this.type = type;
    }


    /**
     * 返回数据统一处理
     *
     * @param value ResponseBody
     * @return value 返回异常
     * @throws IOException 返回数据统一处理
     */
    @Override
    public T convert(ResponseBody value) throws IOException {

        String response = value.string();
        try {
            try {
                boolean isChangeData = false;//是否重新翻修了数据
                if (true) {//把后台的字段重新翻修以下
                    JsonParser jsonParser = new JsonParser();
                    JsonElement jsonElement = jsonParser.parse(response);
                    JsonObject asJsonObject = jsonElement.getAsJsonObject();
                    Iterator<Map.Entry<String, JsonElement>> iterator = asJsonObject.entrySet().iterator();
                    while (iterator.hasNext()) {
                        Map.Entry<String, JsonElement> next = iterator.next();
                        String key = next.getKey();
                        JsonElement value1 = next.getValue();
                        if (TextUtils.equals(key, "msg") && value1.isJsonArray()) {
                            next.setValue(new JsonPrimitive(value1.toString()));
                            isChangeData = true;
                            break;
                        }
                    }
                    if (isChangeData) {
                        response = asJsonObject.toString();
                    }
                    if (response.contains("[]") || response.contains("{}")) {
                        response = response.replaceAll("\\[\\]", "null");
                        response = response.replaceAll("\\{\\}", "null");
                        isChangeData = true;
                        d("--->>>>>>>>---接口请求日志:" + response);
                    }
                }
                d("--->>>>>>>>---是否重新翻修了数据:" + isChangeData);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return gson.fromJson(response, type);
        } finally {
            value.close();
        }


    }
}